package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.order.IChangeRepaymentDateRecordService;
import com.mdd.admin.validate.order.ChangeRepaymentDateRecordCreateValidate;
import com.mdd.admin.validate.order.ChangeRepaymentDateRecordSearchValidate;
import com.mdd.admin.validate.order.ChangeRepaymentDateRecordUpdateValidate;
import com.mdd.admin.vo.order.ChangeRepaymentDateRecordDetailVo;
import com.mdd.admin.vo.order.ChangeRepaymentDateRecordListedVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.ChangeRepaymentDateRecord;
import com.mdd.common.mapper.order.ChangeRepaymentDateRecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 还款日期修改记录实现类
 * @author LikeAdmin
 */
@Service
public class ChangeRepaymentDateRecordServiceImpl implements IChangeRepaymentDateRecordService {

    @Resource
    ChangeRepaymentDateRecordMapper changeRepaymentDateRecordMapper;

    /**
     * 还款日期修改记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderChangeRepaymentDateRecordListedVo>
     */
    @Override
    public PageResult<ChangeRepaymentDateRecordListedVo> list(PageValidate pageValidate, ChangeRepaymentDateRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ChangeRepaymentDateRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        changeRepaymentDateRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:repaymentDateRecordNo@repayment_date_record_no:str",
            "=:repaymentPlanNo@repayment_plan_no:str",
            "=:selectBillingType@select_billing_type:str",
            "=:newRepaymentDate@new_repayment_date:str",
            "=:remarks:str",
        });

        IPage<ChangeRepaymentDateRecord> iPage = changeRepaymentDateRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ChangeRepaymentDateRecordListedVo> list = new LinkedList<>();
        for(ChangeRepaymentDateRecord item : iPage.getRecords()) {
            ChangeRepaymentDateRecordListedVo vo = new ChangeRepaymentDateRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 还款日期修改记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OrderChangeRepaymentDateRecord
     */
    @Override
    public ChangeRepaymentDateRecordDetailVo detail(Long id) {
        ChangeRepaymentDateRecord model = changeRepaymentDateRecordMapper.selectOne(
                new QueryWrapper<ChangeRepaymentDateRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "还款日期修改记录数据不存在");

        ChangeRepaymentDateRecordDetailVo vo = new ChangeRepaymentDateRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 还款日期修改记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(ChangeRepaymentDateRecordCreateValidate createValidate) {
        ChangeRepaymentDateRecord model = new ChangeRepaymentDateRecord();
        model.setRepaymentDateRecordNo(createValidate.getRepaymentDateRecordNo());
        model.setRepaymentPlanNo(createValidate.getRepaymentPlanNo());
        model.setSelectBillingType(createValidate.getSelectBillingType());
        model.setNewRepaymentDate(createValidate.getNewRepaymentDate());
        model.setRemarks(createValidate.getRemarks());
        changeRepaymentDateRecordMapper.insert(model);
    }

    /**
     * 还款日期修改记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(ChangeRepaymentDateRecordUpdateValidate updateValidate) {
        ChangeRepaymentDateRecord model = changeRepaymentDateRecordMapper.selectOne(
                new QueryWrapper<ChangeRepaymentDateRecord>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "还款日期修改记录数据不存在!");

        model.setId(updateValidate.getId());
        model.setRepaymentDateRecordNo(updateValidate.getRepaymentDateRecordNo());
        model.setRepaymentPlanNo(updateValidate.getRepaymentPlanNo());
        model.setSelectBillingType(updateValidate.getSelectBillingType());
        model.setNewRepaymentDate(updateValidate.getNewRepaymentDate());
        model.setRemarks(updateValidate.getRemarks());
        changeRepaymentDateRecordMapper.updateById(model);
    }

    /**
     * 还款日期修改记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        ChangeRepaymentDateRecord model = changeRepaymentDateRecordMapper.selectOne(
                new QueryWrapper<ChangeRepaymentDateRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "还款日期修改记录数据不存在!");

        changeRepaymentDateRecordMapper.delete(new QueryWrapper<ChangeRepaymentDateRecord>().eq("id", id));
    }

}
